$(document).ready(function () {
    $('#crewTable').bootstrapTable({
        url: window._util.BASE_URL + '/Business/QueryCrew',
        method: 'get',
        dataType: "json",
        cache: true,
        totalField: 'MaxRows',
        dataField: 'Items',
        toolbar: '#toolbar',                //工具按钮用哪个容器
        striped: true,                      //是否显示行间隔色
        singleSelect: true,
        pagination: true, //分页
        pageNumber: 1,                       //初始化加载第一页，默认第一页
        pageSize: 20,                       //每页的记录行数（*）
        pageList: [20, 50, 100, 200, 500],        //可供选择的每页的行数（*）
        search: false, //显示搜索框        
        sidePagination: "server", //服务端处理分页       
        columns: [
            {
                field: 'Id',
                title: '编号'
            },
            {
                field: 'Name',
                title: '姓名'
            },
            {
                field: 'Gender',
                title: '性别'
            },
            {
                field: 'IdNo',
                title: '身份证'
            },
            {
                field: 'Type',
                title: '性质'
            },
            {
                field: 'Skill',
                title: '技能'
            },

            {
                field: 'Status',
                title: '状态'
            },
            {
                field: 'PaymentName',
                title: '结算标准'
            },
            {
                field: 'Tel',
                title: '电话'
            },
            {
                field: 'OwnerUnitName',
                title: '所属单位'
            },
            {
                field: 'ServiceUnitName',
                title: '服务单位'
            },
            {
                title: '照片',
                align: 'center',
                formatter: function (value, row, index) {
                    return '<img width="30px" height="30px" src="GetCrewPhoto?id=' + row.Id + "&t=" + new Date().getTime() + '" />';
                }
            },
            {
                title: '操作',
                align: 'center',
                formatter: function (value, row, index) {
                    return '<button data-id="' + row.Id + '" class="schedule btn btn-warning"><i class="fa fa-edit fa-lg"></i> 排 班</button><button data-id="' + row.Id + '" class="edit btn btn-success"> <i class="fa fa-edit fa-lg"></i> 编 辑</button>';
                }
            }
        ]
    });

    var validate = $("div#edit form").validate({
        debug: false,
        focusInvalid: false,
        onkeyup: false,
        rules: {
            Name: {
                required: true
            },
            Gender: {
                required: true
            },
            Type: {
                required: true
            },
            Status: {
                required: true
            },
            Skill: {
                required: true
            },
            Level: {
                required: true
            },
            IdNo: {
                required: true,
                identityCodeValid: true
            },
            OwnerUnitId: {
                required: true
            },
            ServiceUnitId: {
                required: true
            }
        },
        messages: {
            Name: {
                required: "姓名不能为空"
            },
            Gender: {
                required: "性别不能为空"
            },
            Type: {
                required: "性质不能为空"
            },
            Status: {
                required: "状态不能为空"
            },
            Skill: {
                required: "技能不能为空"
            },
            Level: {
                required: "等级不能为空"
            },
            IdNo: {
                required: "身份证不能为空"
            },
            OwnerUnitId: {
                required: "所属单位不能为空"
            },
            ServiceUnitId: {
                required: "服务单位不能为空"
            }

        }

    });

    function B(I) {
        var J = $("div#list table.list tbody");
        J.empty();
        J.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: "CrewPage",
            dataType: "json",
            data: "page=" + I,
            timeout: timeout,
            success: function (K) {
                update(K);
            }
        }).fail(function (L, K) {
            alert("浏览器超时未响应，请刷新重试");
        });
    }
    var pager = {
        maxRows: 0,
        count: 1,
        start: 1,
        display: 20,
        border: true,
        border_color: "#DDD",
        text_color: "#3399D5",
        background_color: "#FFF",
        border_hover_color: "#DDD",
        text_hover_color: "#737373",
        background_hover_color: "#F5F5F5",
        images: false,
        mouse: "press",
        onChange: B
    };
    $("div#lpager").paginate(pager);
    window.update = function (L) {
        var I = $("div#list table.list tbody");
        var P = L.Items;
        $("div#list table.list input.selall").prop("checked", false);
        I.empty();
        var J = L.Page * 10 - 9;
        if (P.length > 0) {
            for (var M = 0; M < P.length; M++) {
                var K = P[M];
                var Q = K.Skill || "";
                var O = K.IdNo || "";
                var N = K.Tel || "";
                I.append('<tr><td><input name=ids class="row" type="checkbox" value="' + K.Id + '"/></td><td>' + K.Name + "</td><td>" + K.Gender + "</td><td>" + O + "</td><td>" + K.Type + "</td><td>" + Q + "</td><td>" + K.Status + "</td><td>" + K.PaymentName + "</td><td>" + N + "</td><td>" + K.OwnerUnitName + "</td><td>" + K.ServiceUnitName + '</td><td><img src="GetCrewPhoto?id=' + K.Id + "&t=" + new Date().getTime() + '" /></td><td><button data-id="' + K.Id + '" class="schedule btn btn-warning"><i class="fa fa-edit fa-lg"></i> 排 班</button></td><td><button data-id="' + K.Id + '" class="edit btn btn-success"> <i class="fa fa-edit fa-lg"></i> 编 辑</button></tr>');
            }
        } else {
            I.append('<tr><td colspan="20">没有相关记录信息</td></tr>');
        }
        pager.maxRows = L.MaxRows;
        pager.start = L.Page;
        pager.count = L.Total;
        $("div#lpager").paginate(pager);
    };

    function editForm(I) {
        $("div#edit input").val("");
        $("div#edit input#file").attr("value", "");
        $("div#edit img").attr("src", "");
        if (I) {
            $("div#edit input.Id").val(I.Id);
            $("div#edit input.Name").val(I.Name);
            $("div#edit input.Birthday").val(I.Birthday);
            $("div#edit input.Native").val(I.Native);
            $("div#edit input.Addr").val(I.Addr);
            $("div#edit input.Tel").val(I.Tel);
            $("div#edit input.WorkStart").val(I.WorkStart);
            $("div#edit input.WorkEnd").val(I.WorkEnd);
            $("div#edit input.Training").val(I.Training);
            $("div#edit input.IdNo").val(I.IdNo);
            $("div#edit input.IdIssuer").val(I.IdIssuer);
            $("div#edit input.TrcNo").val(I.TrcNo);
            $("div#edit input.TrcIssuer").val(I.TrcIssuer);
            $("div#edit input.TrcStart").val(I.TrcStart);
            $("div#edit input.HealthNo").val(I.HealthNo);
            $("div#edit input.HealthIssuer").val(I.HealthIssuer);
            $("div#edit input.HealthStart").val(I.HealthStart);
            $("div#edit input.LicenseNo").val(I.LicenseNo);
            $("div#edit input.LicenseType").val(I.LicenseType);
            $("div#edit input.LicenseIssuer").val(I.LicenseIssuer);
            $("div#edit input.LicenseStart").val(I.LicenseStart);
            $("div#edit input.Note").val(I.Note);
            $("div#edit select.Gender").val(I.Gender);
            $("div#edit select.Skill").val(I.Skill);
            $("div#edit select.Type").val(I.Type);
            $("div#edit select.Status").val(I.Status);
            $("div#edit select.Level").val(I.Level);
            $("div#edit select.OwnerUnitId").val(I.OwnerUnitId);
            $("div#edit select.ServiceUnitId").val(I.ServiceUnitId);
            $("div#edit select.PaymentId").val(I.PaymentId);
            $("div#edit select.LicenseType").val(I.LicenseType);
            $("div#edit td.preview img")[0].src = "GetCrewPhoto?id=" + I.Id + "&t=" + new Date().getTime();
        } else {
            if (window.UserUnit.Id != null) {
                //所属单位
                $("div#edit select.OwnerUnitId").val(window.UserUnit.Id);
                //服务单位
                $("div#edit select.ServiceUnitId").val(window.UserUnit.Id);
            }
        }

        if (window.UserUnit.Role != null && window.UserUnit.Role != '0') {
            //所属单位
            //$("div#edit select.OwnerUnitId").attr("disabled", "disabled");
            //服务单位
            //$("div#edit select.ServiceUnitId").attr("disabled", "disabled");
        } else {
            //所属单位
            //$("div#edit select.OwnerUnitId").removeAttr("disabled");
            //服务单位
            //$("div#edit select.ServiceUnitId").removeAttr("disabled");
        }
    }
    $("div#list div.query button.query").click(function (I) {
        query($("div.query form").serialize(), "QueryCrew");
        return false;
    });
    //query($("div#list div.query form").serialize(), "QueryCrew");
    $("div#list ul.tool button.add").click(function (I) {
        editForm(null);
        select("edit");
        return false;
    });

    $("div#edit form").ajaxForm({
        dataType: "json",
        success: function (I) {
            alert(I.Message);
            if (I.Result) {
                select("list");
                query("keep=true", "QueryCrew");
            }
        },
        error: function (J, I, K) {
            alert("浏览器超时未响应，请刷新重试");
        }
    });
    $("button.cancel").click(function (I) {
        validate.resetForm();
        select("list");
        return false;
    });
    $("div#list ul.tool button.delete").click(function (I) {
        if (confirm("您确定要删除所有选中的记录吗？")) {
            var J = $("div#list table.list tbody input.row:checked").serialize();
            $.ajax({
                url: "DeleteCrews",
                dataType: "json",
                data: J,
                timeout: timeout,
                success: function (K) {
                    alert(K.Message);
                    if (K.Result) {
                        query("keep=true", "QueryCrew");
                    }
                }
            }).fail(function (L, K) {
                alert("浏览器超时未响应，请刷新重试");
            });
        }
    });
    $("div#list table.list tbody").click(function (I) {
        var J = $(I.target);
        if (J.is("button.edit")) {
            var K = J.data("id");
            $.ajax({
                url: "GetCrew",
                dataType: "json",
                data: "Id=" + K,
                timeout: timeout,
                success: function (L) {
                    editForm(L);
                    select("edit");
                }
            }).fail(function (M, L) {
                alert("浏览器超时未响应，请刷新重试");
            });
        } else {
            if (J.is("button.schedule")) {
                $("div#schedule div.query input.CrewId").val(J.data("id"));
                A($("div#schedule div.query form").serialize(), "QuerySchedule");
                select("schedule");
                $("div#spager").paginate(pagerSchedule);
            }
        }
    });

    function E(J) {
        var I = new FileReader();
        I.onload = function (K) {
            $("div#edit td.preview img")[0].src = K.target.result
        };
        I.readAsDataURL(J);
    }
    $("div#edit input#file").change(function () {
        if (this.files) {
            E(this.files[0]);
        } else {
            alert("您的浏览器不支持HTML5, 无法预览图片");
        }
    });

    function F(I) {
        var J = $("div#schedule table.list tbody");
        J.empty();
        J.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: "SchedulePage",
            dataType: "json",
            data: "page=" + I,
            timeout: timeout,
            success: function (K) {
                G(K);
            }
        }).fail(function (L, K) {
            alert("浏览器超时未响应，请刷新重试");
        });
    }
    var pagerSchedule = {
        maxRows: 0,
        count: 1,
        start: 1,
        display: 20,
        border: true,
        border_color: "#DDD",
        text_color: "#3399D5",
        background_color: "#FFF",
        border_hover_color: "#DDD",
        text_hover_color: "#737373",
        background_hover_color: "#F5F5F5",
        images: false,
        mouse: "press",
        onChange: F
    };

    function G(K) {
        var I = $("div#schedule table.list tbody");
        var O = K.Items;
        $("div#schedule table.list input.selall").prop("checked", false);
        I.empty();
        if (O.length > 0) {
            for (var L = 0; L < O.length; L++) {
                var J = O[L];
                var N = J.Adjust || "";
                var M = J.Rate || "";
                var P = J.Note || "";
                I.append('<tr><td><input name=ids class="row" type="checkbox" value="' + J.Id + '"/></td><td>' + J.CustomerName + "</td><td>" + J.ServiceName + "</td><td>" + $.toSpace(J.Start) + "</td><td>" + J.ShiftName + "</td><td>" + J.Ratio + "</td><td>" + N + "</td><td>" + J.Pay + "</td><td>" + M + "</td></tr>");
            }
        } else {
            I.append('<tr><td colspan="20">没有相关记录信息</td></tr>');
        }
        pagerSchedule.maxRows = K.MaxRows;
        pagerSchedule.start = K.Page;
        pagerSchedule.count = K.Total;
        $("div#spager").paginate(pagerSchedule);
    }
    function A(J, I) {
        var K = $("div#schedule table.list tbody");
        K.empty();
        K.append('<tr><td colspan="20">数据加载中...</td></tr>');
        $.ajax({
            url: I,
            dataType: "json",
            data: J,
            timeout: timeout,
            success: function (L) {
                G(L);
            },
            error: function (M, L, N) {
                alert("浏览器超时未响应，请刷新重试");
            }
        });
    }
    $("div#schedule div.query button.query").click(function (I) {
        A($("div#schedule div.query form").serialize(), "QuerySchedule");
        return false;
    });
    $("div#schedule ul.tool button.delete").click(function (I) {
        if (confirm("您确定要删除所有选中的记录吗？")) {
            var J = $("div#schedule table.list tbody input.row:checked").serialize();
            $.ajax({
                url: "DeleteSchedules",
                dataType: "json",
                data: J,
                timeout: timeout,
                success: function (K) {
                    alert(K.Message);
                    if (K.Result) {
                        A("keep=true", "QuerySchedule");
                    }
                }
            }).fail(function (L, K) {
                alert("浏览器超时未响应，请刷新重试");
            });
        }
    });
});